<?php

namespace app\admin\controller\setting;

use app\admin\logic\system\SystemConfigLogic;
use app\basic\BaseController;
use exception\ApiException;
use support\Request;
use support\Response;
use think\Exception;
use support\utils\Cache;

class PayConfigController extends BaseController
{
    /**
     * 构造
     */
    public function __construct()
    {
        $this->logic = new SystemConfigLogic();
        parent::__construct();
    }

    /**
     * 读取支付配置信息
     */
   public function redaPayConfig(Request $request)
   {
       $param = $request->post();
       return $this->success($this->logic->getConfig($param['key'])['value']);
   }

    /**
     * 编辑支付配置信息
     * @param Request $request
     * @return \support\Response
     */
    public function editPayConfig(Request $request)
    {
        $param = $request->post();
        $paySetting = $this->logic->getConfig($param['key'])['value'];
        // 基础配置
        if($param['key'] === 'base_pay_setting'){
            // 先读取当前配置的信息
            $value = json_decode($param['value'], true);
            $param['value'] = json_encode(array_merge($paySetting,$value),JSON_UNESCAPED_UNICODE);
        }
        try {
            if ($this->logic->where(['key' => $param['key']])->update(['value' => $param['value']])) {
                // 删除缓存
                Cache::clear('config_'.$param['key']);
                return $this->success("修改成功");
            } else {
                throw new ApiException("更新失败");
            }
        } catch (Exception $e) {
            return $this->fail("修改失败: " . $e->getMessage());
        }
    }

}